<?php

namespace app\service\statistics;

class Product
{
    public function __construct()
    {
        $this->product        = new \app\admin\model\product\Product();
        $this->orderItem      = new \app\admin\model\order\OrderItem();
        $this->order          = new \app\common\model\Order();
        $this->orderAftersale = new \app\admin\model\order\Aftersale();
        $this->productView    = new \app\admin\model\product\ProductViews();
    }

    public function statistics()
    {
        //累计商品浏览量
        $product_view = $this->productView
            ->cache(60)
            ->count();
        //今日商品浏览量
        $today_product_view = $this->productView
            ->whereDay('update_time')
            ->cache(60)
            ->count();
        //今日访客数
        $today_user_view = $this->productView
            ->whereDay('update_time')
            ->group('user_id')
            ->cache(60)
            ->count();
        //今日支付件数
        $today_item_count = $this->orderItem
            ->whereDay('create_time')
            ->cache(60)
            ->count();
        //昨天支付件数
        $yesterday_item_count = $this->orderItem
            ->whereDay('create_time', 'yesterday')
            ->cache(true)
            ->count();
        //近七日支付件数
        $seven_item_count = $this->orderItem->view('OrderItem')
            ->view('Order', '', 'OrderItem.order_id = Order.id')
            ->where('Order.pay_time', '>=', time_stamp_start(0, 7, 0))
            ->where('Order.pay_time', '<=', time_stamp_end(0, 0, 0))
            ->cache(60)->count();
        //前七日支付件数
        $fourteen_item_count = $this->orderItem->view('OrderItem')
            ->view('Order', '', 'OrderItem.order_id = Order.id')
            ->where('Order.pay_time', '>=', time_stamp_start(0, 14, 0))
            ->where('Order.pay_time', '<=', time_stamp_end(0, 7, 0))
            ->cache(true)->count();

        //昨天收入
        $yesterday_money = $this->order
            ->where('pay_time', '>=', time_stamp_start(0, 1, 0))
            ->where('pay_time', '<=', time_stamp_end(0, 1, 0))
            ->cache(true)
            ->sum('remain_pay_fee');
        //今天收入
        $today_money = $this->order
            ->where('pay_time', '>=', time_stamp_start(0, 0, 0))
            ->where('pay_time', '<=', time_stamp_end(0, 0, 0))
            ->sum('remain_pay_fee');
        //近七日收入
        $seven_money = $this->order
            ->where('pay_time', '>=', time_stamp_start(0, 7, 0))
            ->where('pay_time', '<=', time_stamp_end(0, 0, 0))
            ->cache(60)->sum('remain_pay_fee');
        //前七日收入
        $fourteen_money = $this->order
            ->where('pay_time', '>=', time_stamp_start(0, 14, 0))
            ->where('pay_time', '<=', time_stamp_end(0, 7, 0))
            ->cache(true)->sum('remain_pay_fee');

        $yesterday_item_count_rate = '0.00';
        if ($yesterday_item_count > 0) {
            $yesterday_item_count_rate = bcdiv(bcsub($today_item_count, $yesterday_item_count, 2) * 100, $yesterday_item_count, 2) ?: '0.00';
        }
        $fourteen_item_count_rate = '0.00';
        if ($fourteen_item_count > 0) {
            $fourteen_item_count_rate = bcdiv(bcsub($seven_item_count, $fourteen_item_count, 2) * 100, $fourteen_item_count, 2) ?: '0.00';
        }

        //今日退款件数
        $today_item_refund = $this->orderAftersale
            ->whereDay('create_time')
            ->cache(60)
            ->count();
        //昨天退款件数
        $yesterday_item_refund = $this->orderAftersale
            ->whereDay('create_time', 'yesterday')
            ->cache(true)
            ->count();
        //近七日退款件数
        $seven_item_refund = $this->orderAftersale
            ->where('create_time', '>=', time_stamp_start(0, 7, 0))
            ->where('create_time', '<=', time_stamp_end(0, 0, 0))
            ->cache(60)->count();
        //前七日退款件数
        $fourteen_item_refund = $this->orderAftersale
            ->where('create_time', '>=', time_stamp_start(0, 14, 0))
            ->where('create_time', '<=', time_stamp_end(0, 7, 0))
            ->cache(true)->count();

        $yesterday_item_refund_rate = '0.00';
        if ($yesterday_item_refund > 0) {
            $yesterday_item_refund_rate = bcdiv(bcsub($today_item_count, $yesterday_item_refund, 2) * 100, $yesterday_item_refund, 2) ?: '0.00';
        }
        $fourteen_item_refund_rate = '0.00';
        if ($fourteen_item_refund > 0) {
            $fourteen_item_refund_rate = bcdiv(bcsub($seven_item_refund, $fourteen_item_refund, 2) * 100, $fourteen_item_refund, 2) ?: '0.00';
        }

        return [
            'product_view'               => $product_view,
            'total_product'              => $this->product->count(),
            'today_product_view'         => $today_product_view,
            'today_user_view'            => $today_user_view,
            'today_item_count'           => $today_item_count,
            'yesterday_item_count_rate'  => $yesterday_item_count_rate,
            'fourteen_item_count_rate'   => $fourteen_item_count_rate,
            'today_money'                => bcmul($today_money, 1, 2),
            'yesterday_money_rate'       => $yesterday_money ? bcdiv(bcsub($today_money, $yesterday_money, 2) * 100, $yesterday_money, 2) : '0.00',
            'seven_money_rate'           => $fourteen_money ? bcdiv(bcsub($seven_money, $fourteen_money, 2) * 100, $fourteen_money, 2) : '0.00',
            'today_item_refund'          => $today_item_refund,
            'yesterday_item_refund_rate' => $yesterday_item_refund_rate,
            'fourteen_item_refund_rate'  => $fourteen_item_refund_rate,
        ];
    }
}
